package com.king.mvpdemo.main;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;

import com.king.mvpdemo.R;
import com.king.mvpdemo.base.BaseMvpActivity;
import com.king.mvpdemo.main.adapter.FragmentViewPagerAdapter;
import com.king.mvpdemo.main.contract.MainContract;
import com.king.mvpdemo.main.presenter.MainPresenter;
import com.king.mvpdemo.newfragment.NewFragment;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.ColorTransitionPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.SimplePagerTitleView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @Author King
 * @Date 2019/8/7 10:47
 */
public class MainActivity extends BaseMvpActivity<MainPresenter> implements MainContract.View {


    @BindView(R.id.magic_indicator7)
    MagicIndicator magicIndicator7;
    @BindView(R.id.viewPager)
    ViewPager viewPager;
    private List<String> titles = new ArrayList<>();
    private FragmentViewPagerAdapter fragmentViewPagerAdapter;
    private List<Fragment> fragmentList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);//UI绑定

    }

    public void setTitles() {
        titles.add("头条");
        titles.add("社会");
        titles.add("国内");
        titles.add("国际");
        titles.add("娱乐");
        titles.add("体育");
        titles.add("军事");
        titles.add("科技");
        titles.add("财经");
        titles.add("时尚");
    }


    /**
     * 设置top滑动菜单
     */
    public void setMagicIndicator7() {
        setTitles();
        fragmentList = new ArrayList<>();
        for (int i=0;i<titles.size();i++){
            fragmentList.add(new NewFragment().newInstance(titles.get(i)));
        }
        fragmentViewPagerAdapter = new FragmentViewPagerAdapter(getSupportFragmentManager(),fragmentList);
        viewPager.setAdapter(fragmentViewPagerAdapter);
        CommonNavigator commonNavigator = new CommonNavigator(this);
        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return titles.size();
            }
            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
                simplePagerTitleView.setText(titles.get(index));
                simplePagerTitleView.setTextSize(16);//设置导航的文字大小
                simplePagerTitleView.setNormalColor(Color.GRAY);//正常状态下的标题颜色
                simplePagerTitleView.setSelectedColor(Color.parseColor("#00c853"));//选中的标题字体颜色
                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        viewPager.setCurrentItem(index);
                    }
                });
                return simplePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator  indicator = new LinePagerIndicator(context);
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                indicator.setLineHeight(Float.valueOf(UIUtil.dip2px(context, 6.0)));//下划线高度
                indicator.setLineWidth(Float.valueOf(UIUtil.dip2px(context, 20.0)));//下划线宽度
                indicator.setRoundRadius(Float.valueOf(UIUtil.dip2px(context, 3.0)));//下划线圆角
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(2.0f));
                indicator.setColors(Color.parseColor("#00c853"));
                return indicator;
            }
        });
        magicIndicator7.setNavigator(commonNavigator);
        ViewPagerHelper.bind(magicIndicator7, viewPager);
    }

    @Override
    public int getLayoutId() {
        return R.layout.new_list_activity;
    }

    @TargetApi(Build.VERSION_CODES.N)
    @Override
    public void initView() {//初始化
        mPresenter = new MainPresenter();
        mPresenter.attachView(this);
        setMagicIndicator7();
    }

    @Override
    public void showLoading() {


    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void onError(Throwable throwable) {

    }

    @Override
    public void onSuccess() {
    }


}
